import yaml
from appium.webdriver.common.appiumby import AppiumBy
from appium import webdriver

# search_yaml_data = yaml.safe_load(open("demo_data_driven.yaml", encoding="utf-8", mode="r"))
# print(search_yaml_data)

# print("hello world")
file = open("../Hybrid混合APP的自动化测试/demo_process_driven.yaml", "r", encoding="utf-8")
print(yaml.safe_load(file))
# steps = yaml.safe_load(file)
# print(steps)


class TestCase:
    def __init__(self, path):
        file_name = open(path, "r", encoding="utf-8")
        self.steps = yaml.safe_load(file_name)
        print(self.steps)

    def run(self, driver: webdriver):
        for step in self.steps:
            element = None

            if isinstance(step, dict):
                if "id" in step.keys():
                    element = driver.find_element(by=AppiumBy.ID, value=step['id'])
                elif "xpath" in step.keys():
                    element = driver.find_element(by=AppiumBy.XPATH, value=step['xpath'])
                elif "class_name" in step.keys():
                    element = driver.find_element(by=AppiumBy.CLASS_NAME, value=step['class_name'])
                else:
                    print(step.keys())

                if "input" in step.keys():
                    element.send_keys(step["input"])
                elif "get" in step.keys():
                    element.get_attribute(step["get"])
                else:
                    element.click()

